﻿using System;
using System.Collections.Generic;
using System.Linq;
using Autodesk.Revit.DB;
using YArchitech.Revit;
using YJKArchUtils.Utils;
using YJKRevitDimensionText.OneKeyPlanDim.Utility;

namespace YJKRevitDimensionText.OneKeyPlanDim.DimData
{
	public class DbRoofRamp : DbDimBase
	{
		public List<RoofBase> LstRoofBase
		{
			set
			{
				this.m_lstRoofBase = value;
			}
		}

		public List<Floor> LstFloor
		{
			set
			{
				this.m_lstFloor = value;
			}
		}

		public DbRoofRamp(Autodesk.Revit.DB.Document doc)
		{
			this.m_doc = doc;
		}

		public void CreateSlopeDim()
		{
			this.m_bOutArch = false;
			if (!base.CreateSlopeDim(this.m_doc))
			{
				return;
			}
			List<Element> list = new List<Element>();
			list.AddRange(this.m_lstRoofBase);
			list.AddRange(this.m_lstFloor);
			SlopeDimData slopeDimData = new SlopeDimData(this.m_doc);
			foreach (Element thisElement in list)
			{
				foreach (PlanarFace planarFace in GeometryHelper.GetAllPlanarFace(thisElement))
				{
					XYZ faceNormal = planarFace.FaceNormal;
					if (!Geometry.IsVertical(faceNormal, XYZ.BasisZ, 1E-09) && !Geometry.LessThan(faceNormal.Z, 0.0))
					{
						List<CurveLoop> list2 = planarFace.GetEdgesAsCurveLoops().ToList<CurveLoop>();
						if (list2.Count >= 1)
						{
							XYZ xyz = null;
							CurveArray curveArray = new CurveArray();
							foreach (Curve curve in list2[0])
							{
								curveArray.Append(curve);
							}
							try
							{
								xyz = YJKRevitTools.Centroid(curveArray);
							}
							catch
							{
								continue;
							}
							if (xyz != null)
							{
								XYZ xyz2 = faceNormal.CrossProduct(XYZ.BasisZ);
								XYZ xyz3 = faceNormal.CrossProduct(xyz2);
								if (!Geometry.LessThan(xyz3.AngleTo(faceNormal), 0.10122909661567112))
								{
									XYZ xyz4 = xyz + xyz3 * 1.5;
									if (!Geometry.LessThan(xyz.Z, xyz4.Z))
									{
										string dimVal = "";
										this.CalSlopeValue(planarFace, out dimVal);
										slopeDimData.SlopeDimDir = xyz3;
										slopeDimData.PtStart = xyz;
										slopeDimData.DimVal = dimVal;
										FamilyInstance familyInstance = slopeDimData.CreateSlopeDim();
										if (familyInstance != null)
										{
											this.SetDimPosition(familyInstance);
										}
									}
								}
							}
						}
					}
				}
			}
		}

		public void SetDimPosition(FamilyInstance familyDim)
		{
			CurveLoop curveLoop = OcclusionArea.GetSlopeDimCurveLoop(familyDim);
			LocationPoint locationPoint = familyDim.Location as LocationPoint;
			if (locationPoint == null)
			{
				return;
			}
			XYZ point = locationPoint.Point;
			XYZ ptEnd = point;
			List<CurveLoop> list = new List<CurveLoop>();
			list.AddRange(OcclusionArea.GetInstance().m_lstWallCvLoop);
			list.AddRange(OcclusionArea.GetInstance().m_lstOpeningCvLoop);
			for (int i = 0; i < 17; i++)
			{
				ptEnd = CommonInterface.GetNumCorrespondingOffsetPoint(16, i, Common.MMToFeet(600.0), point);
				CurveLoop curveLoop2 = CommonInterface.MoveCurve(curveLoop, point, ptEnd);
				if (!CommonInterface.IsExistOcclusion(curveLoop2, list))
				{
					curveLoop = curveLoop2;
					break;
				}
				if (16 == i)
				{
					ptEnd = point;
					break;
				}
			}
			CommonInterface.MoveElement(familyDim, point, ptEnd, this.m_doc);
			OcclusionArea.GetInstance().m_lstSlopeDimCvLoop.Add(curveLoop);
		}

		private void CalSlopeValue(PlanarFace StartBasePlanarFace, out string strSlopeValue)
		{
			double num = StartBasePlanarFace.FaceNormal.AngleTo(XYZ.BasisZ);
			if (Geometry.LessThan(Math.PI*.5, num))
			{
				num = Math.PI - num;
			}
			strSlopeValue = (Math.Tan(num) * 100.0).ToString("f0") + "%";
		}

		private List<RoofBase> m_lstRoofBase = new List<RoofBase>();

		private List<Floor> m_lstFloor = new List<Floor>();
	}
}
